package com.csinc.generate.core.make.impl;

import com.csinc.generate.core.make.MakeProject;
import com.csinc.generate.core.util.FreeMakerUtil;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateHashModel;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Create By 郭金宝 on 2018/7/25.<br>
 */
@Slf4j
public abstract class MakeProjectMaker<T> extends MakeProject<T> {

    protected Configuration cfg = new Configuration();


    protected Map<Object, Object> data = new HashMap<Object, Object>();

    protected String ftl;

    public abstract void data();

    public MakeProjectMaker() {
        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
        TemplateHashModel staticModels = wrapper.getStaticModels();
        TemplateHashModel fileStatics;
        try {
            fileStatics = (TemplateHashModel) staticModels.get(FreeMakerUtil.class.getName());
            cfg.setSharedVariable("util", fileStatics);
            cfg.setDefaultEncoding("UTF-8");
            String ftlPath = MakeProjectMaker.class.getResource("/freemaker/boot/mapper.ftl").getPath();
//			cfg.setDirectoryForTemplateLoading(file.getParentFile());
            cfg.setClassForTemplateLoading(MakeProjectMaker.class, "/freemaker/boot");
            cfg.setObjectWrapper(new DefaultObjectWrapper());
        } catch (Exception e) {
            log.error("freemaker init error!!!!!!!!!!!!1", e);
            e.printStackTrace();
        }

    }

    protected String toString(String ftl, Map<Object, Object> root) {
        try {
            StringWriter writer = new StringWriter();
            Template temp = cfg.getTemplate(ftl);
            temp.process(root, writer);
            return writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return "";
    }

    protected void out(String ftl, Map<Object, Object> root) {
        OutputStream out = null;
        try {
            check(outPath);
            out = new FileOutputStream(new File(outPath, fileName));
            OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");
            Template temp = cfg.getTemplate(ftl);
            temp.process(root, writer);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public T ftl(String ftl) {
        this.ftl = ftl;
        return ((T) this);
    }

    private void check(String path) {
        File f = new File(path);
        while(!f.exists()) {
            f.mkdirs();
        }
    }
}